package com.solomon.travel.common.util;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * @ClassName:StringUtil
 * @Description:字符串工具类
 * @author Mark
 * @date 2011-12-2 上午11:12:14
 */
public class StringUtil {

	/**
	 * @Title: isNull
	 * @Description: 判断字符串是否为空
	 * @param @param s
	 * @param @return
	 * @return boolean
	 * @throws
	 */
	public static boolean isEmpty(String s) {
		if (s == null) {
			return true;
		}
		s = s.trim();
		return (s.equals(StringPool.BLANK) || s.equals(StringPool.NULL));
	}

	/**
	 * @Title: isNotEmpty
	 * @Description: 判断字符串是否不为空
	 * @param @param s
	 * @param @return
	 * @return boolean
	 * @throws
	 */
	public static boolean isNotEmpty(String s) {
		return !isEmpty(s);
	}

	/**
	 * @Title: first2Upper
	 * @Description: 字符串首字母转大写
	 * @param @param s
	 * @return void
	 * @throws
	 */
	public static String first2Upper(String s) {
		String temp = getFirst(s);
		return (!ValidateUtil.isChar(temp)) ? null : temp.toUpperCase();
	}

	/**
	 * @Title: first2Upper
	 * @Description: 根据表字段名称获取规格化的属性名称:去掉下划线，非首单词的首字符大写
	 * @param @param s
	 * @param @param underLine
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String first2Upper(String s, String underLine) {
		StringBuffer bs = new StringBuffer();
		String[] array = split(s.toLowerCase(), underLine);
		bs.append(array[0]);// 先取一个字符串
		for (int i = 0; i < array.length; i++) {
			bs.append(array[i].substring(0, 1).toUpperCase())// 首字大写
					.append(array[i].substring(1, array[i].length()));// 其他字符不变
		}
		return bs.toString();
	}

	/**
	 * @Title: getFirst
	 * @Description:取字符串首字符
	 * @param @param s
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String getFirst(String s) {
		return isEmpty(s) ? null : s.substring(0, 1);
	}

	/**
	 * @Title: removeLastChar
	 * @Description: 删除最后一个字符
	 * @param @param s
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String removeLastChar(String s) {
		return isEmpty(s) ? null : s.substring(0, s.length() - 1);
	}

	/**
	 * @Title: shorten
	 * @Description: 截取长度为20,带上后缀...
	 * @param @param s
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String shorten(String s) {
		return shorten(s, 20);
	}

	/**
	 * @Title: shorten
	 * @Description: 截取长度为20,带上后缀...
	 * @param @param s
	 * @param @param length
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String shorten(String s, int length) {
		return shorten(s, 20, "...");
	}

	/**
	 * @Title: shorten
	 * @Description: 截取指定长度的字符串并带上后缀
	 * @param @param s
	 * @param @param length
	 * @param @param suffix
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String shorten(String s, int length, String suffix) {
		if (isEmpty(s) || isEmpty(suffix)) {
			return null;
		}
		if (s.length() > length) {// 如果字符串长度大于指定截取的长度
			s = s.substring(0, length) + suffix;
		}
		return s;
	}

	/**
	 * @Title: split
	 * @Description: 根据逗号分割字符串
	 * @param @param s
	 * @param @return
	 * @return String[]
	 * @throws
	 */
	public static String[] split(String s) {
		return split(s, ",");
	}

	/**
	 * @Title: split
	 * @Description: 根据指定字符串分割字符串
	 * @param @param s
	 * @param @param delimiter
	 * @param @return
	 * @return String[]
	 * @throws
	 */
	public static String[] split(String s, String delimiter) {
		return s.split(delimiter);
	}

	/**
	 * @Title: split
	 * @Description: 分割指定字符串为整型数组,不为整形的字符串赋默认值x
	 * @param @param s
	 * @param @param delimiter
	 * @param @param x
	 * @param @return
	 * @return int[]
	 * @throws
	 */
	public static int[] split(String s, String delimiter, int x) {
		String[] array = s.split(delimiter);
		int[] newArray = new int[array.length];
		for (int i : newArray) {
			int value = x;// 赋默认值
			try {
				value = Integer.parseInt(array[i]);// 将整形字符串转换为整形
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
			newArray[i] = value;
		}
		return newArray;
	}

	/**
	 * @Title: split
	 * @Description: 将字符串转换为boolean型数组
	 * @param @param s
	 * @param @param delimiter
	 * @param @param x
	 * @param @return
	 * @return boolean[]
	 * @throws
	 */
	public static boolean[] split(String s, String delimiter, boolean x) {
		String[] array = split(s, delimiter);
		boolean[] newArray = new boolean[array.length];
		for (int i = 0; i < array.length; i++) {
			boolean value = x;
			try {
				value = Boolean.valueOf(array[i]).booleanValue();
			} catch (Exception e) {
				e.printStackTrace();
			}
			newArray[i] = value;
		}
		return newArray;
	}

	/**
	 * @Title: split
	 * @Description: TODO
	 * @param @param s
	 * @param @param delimiter
	 * @param @param x
	 * @param @return
	 * @return double[]
	 * @throws
	 */
	public static double[] split(String s, String delimiter, double x) {
		String[] array = split(s, delimiter);
		double[] newArray = new double[array.length];
		for (int i = 0; i < array.length; i++) {
			double value = x;
			try {
				value = Double.parseDouble(array[i]);
			} catch (Exception e) {
				e.printStackTrace();
			}
			newArray[i] = value;
		}
		return newArray;
	}

	/**
	 * @Title: split
	 * @Description: 将字符串转换为float型数组
	 * @param @param s
	 * @param @param delimiter
	 * @param @param x
	 * @param @return
	 * @return float[]
	 * @throws
	 */
	public static float[] split(String s, String delimiter, float x) {
		String[] array = split(s, delimiter);
		float[] newArray = new float[array.length];
		for (int i = 0; i < array.length; i++) {
			float value = x;
			try {
				value = Float.parseFloat(array[i]);
			} catch (Exception e) {
				e.printStackTrace();
			}
			newArray[i] = value;
		}
		return newArray;
	}

	/**
	 * @Title: split
	 * @Description: 将字符串转成short型数组
	 * @param @param s
	 * @param @param delimiter
	 * @param @param x
	 * @param @return
	 * @return short[]
	 * @throws
	 */
	public static short[] split(String s, String delimiter, short x) {
		String[] array = split(s, delimiter);
		short[] newArray = new short[array.length];
		for (int i = 0; i < array.length; i++) {
			short value = x;
			try {
				value = Short.parseShort(array[i]);
			} catch (Exception e) {
				e.printStackTrace();
			}
			newArray[i] = value;
		}

		return newArray;
	}

	/**
	 * @Title: split
	 * @Description: 将字符串转成Long型数组
	 * @param @param s
	 * @param @param delimiter
	 * @param @param defaultValue
	 * @param @return
	 * @return Long[]
	 * @throws
	 */
	public static Long[] split(String s, String delimiter, Long defaultValue) {
		String[] array = split(s, delimiter);
		Long[] newArray = new Long[array.length];
		for (int i = 0; i < array.length; i++) {
			Long value = defaultValue;
			try {
				value = new Long(array[i]);
			} catch (Exception e) {
				e.printStackTrace();
			}
			newArray[i] = value;
		}
		return newArray;
	}

	/**
	 * @Title: add
	 * @Description: 新增目标字符串
	 * @param @param s 原始字符串
	 * @param @param add 目标字符串
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String add(String s, String add) {
		return add(s, add, StringPool.COMMA);
	}

	/**
	 * @Title: add
	 * @Description: 新增目标字符串
	 * @param @param s 原始字符串
	 * @param @param add 目标字符串
	 * @param @param delimiter 分隔符
	 * @param @return
	 * @return String 默认允许重复
	 * @throws
	 */
	public static String add(String s, String add, String delimiter) {
		return add(s, add, delimiter, false);
	}

	/**
	 * @Title: add
	 * @Description: 新增目标字符串
	 * @param @param s 原始字符串
	 * @param @param add 新增加的字符串
	 * @param @param delimiter 分割符
	 * @param @param allowDuplicates 是否允许重复
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String add(String s, String add, String delimiter,
			boolean allowDuplicates) {
		if (add == null || delimiter == null) {// 如果新增加的字符串或者分割符为null,则直接返回null
			return null;
		}
		if (s == null) {// 如果原始字符串为null,则赋空字符串
			s = StringPool.BLANK;
		}
		if (allowDuplicates || !contains(s, add, delimiter)) {
			if (isEmpty(s) || s.endsWith(delimiter)) {
				s += add + delimiter;
			} else {
				s += delimiter + add + delimiter;
			}
		}
		return s;
	}

	/**
	 * @Title: contains
	 * @Description: 判断字符串是否包含另一字符串,默认带有逗号分隔符
	 * @param @param s
	 * @param @param text
	 * @param @return
	 * @return boolean
	 * @throws
	 */
	public static boolean contains(String s, String text) {
		return contains(s, text, StringPool.COMMA);
	}

	/**
	 * @Title: contains
	 * @Description: 字符串是否包含另一字符串
	 * @param @param s 原始字符串
	 * @param @param text 被包含的字符串
	 * @param @param delimiter 分割符,可定义相关分隔符
	 * @param @return
	 * @return boolean
	 * @throws
	 */
	public static boolean contains(String s, String text, String delimiter) {
		if (s == null || text == null || delimiter == null) {
			return false;
		}
		if (!s.endsWith(delimiter)) {// 判断字符串是否以分割符结束
			s += delimiter;// 如果没有分割符则加上分割符
		}
		int pos = s.indexOf(delimiter + text + delimiter);
		if (pos == -1) {
			return s.startsWith(text + delimiter);// 字符串是否包含指定内容
		}
		return true;
	}

	/**
	 * @Title: count
	 * @Description: 计算目标字符串在原始字符串出现的次数
	 * @param @param s 源字符串
	 * @param @param text 目标字符串
	 * @param @return
	 * @return int
	 * @throws
	 */
	public static int count(String s, String text) {
		if (isEmpty(s) || isEmpty(text)) {
			return 0;
		}
		int count = 0;
		int pos = s.indexOf(text);
		while (pos != -1) {
			pos = s.indexOf(text, pos + text.length());
			count++;
		}
		return count;
	}

	/**
	 * @Title: merge
	 * @Description: 合并数组里的字符串元素,默认逗号隔离
	 * @param @param array
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String merge(String[] array) {
		return merge(array, StringPool.COMMA);
	}

	/**
	 * @Title: merge
	 * @Description: 合并数组里的字符串元素
	 * @param @param array
	 * @param @param delimiter
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String merge(String[] array, String delimiter) {
		if (array == null) {
			return null;
		}
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < array.length; i++) {// 遍历数组
			sb.append(array[i].trim());
			if ((i + 1) != array.length) {// 如果下标不等于数组长度,即表示最后一个元素
				sb.append(delimiter);
			}
		}
		return sb.toString();
	}

	/**
	 * @Title: merge
	 * @Description: 将集合类合并,默认逗号分搁
	 * @param @param coll
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String merge(Collection<?> coll) {
		return merge(coll, StringPool.COMMA);
	}

	/**
	 * @Title: merge 将集合合并成字符串
	 * @Description: 合并字符串
	 * @param @param coll 集合类
	 * @param @param delimiter 分隔符
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String merge(Collection<?> coll, String delimiter) {
		StringBuffer sb = new StringBuffer();
		if (CollectionUtil.isNotNull(coll)) {
			Iterator<?> it = coll.iterator();
			while (it.hasNext()) {
				sb.append(it.next()).append(delimiter);
			}
			return sb.toString().substring(0, sb.length() - 1);
		} else {
			return null;
		}
	}
	
	
	public static String toSqlString(String[] ids,boolean hasQuote){
		StringBuffer str = new StringBuffer ();
		for(String s : ids){
			str.append(hasQuote ? "'"+s+"'" : s).append(",");
		}
		
		if ( str.indexOf ( "," ) > 0 ) {
			str.deleteCharAt ( str.length () - 1 );
		}
		return str.toString();
	}
	
	public static String stringFilter(String str) throws PatternSyntaxException {
		String regEx = "[`~!@#$^&*()+=|{}':;',\\[\\].<>/?~！@#￥……&*（）——+|{}【】‘；：”“’。，、？\\s*|\t|\r|\n]";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		return m.replaceAll("").trim();
	}	
	
	public static String format(Map<String, String> map){
	
		return null;
	}

	public static void main(String[] args) {
		System.out.println(contains("dddd-aaaa-bbbb-cccc", "aaaa-bbbb", "-"));
	}
}
